<template>
  <div class="menu">
    <el-card style="width:100%">
      <template #header>
        <div class="dep_title">
          菜单树
          <div class="right">
            <el-button size="small" @click="append()" :icon="Plus">添加菜单</el-button>
            <el-button :icon="Delete" size="small" type="primary" @click="deleteMenu()"> 批量删除 </el-button>
          </div>
        </div>
        
      </template>
      <el-tree
        ref="treeRef"
        :data="menuItems"
        :props="defaultProps"
        show-checkbox
        highlight-current
      >
        <template #default="{ node, data }">
        <div class="custom-tree-node">
          <span>{{ node.label }}</span>
          <span>
             <el-switch
            v-model="node.state"
            inline-prompt
            active-text="禁止"
            inactive-text="启用"
          />
       
            <el-button text size="small" :icon="Plus" @click="append(data)">新增下级</el-button>
            <el-button text size="small" @click="editMenu(data)" :icon="Edit">编辑</el-button>
            
              
              <el-button :icon="Delete" size="small" type="danger" text @click="deleteMenu(node,data)"> 删除 </el-button>
          </span>
        </div>
      </template>
      </el-tree>
    </el-card>
    <Dialog
      :isVisible="isVisible"
      :title="title"
      @submit="submitMenu"
      @close="cancel"
    >
      <!-- form 表单组件 -->
      <MenuForm :formData="formData" />
    </Dialog>
  </div>
</template>

<script setup>
import Dialog from "@/components/Dialog.vue";
import MenuForm from "./modal/form.vue";
import { Plus, MessageBox, Edit, Delete } from "@element-plus/icons-vue";
import { computed, onMounted, reactive, ref } from "vue";
const operate = ref(true)

const menuItems = ref([])

onMounted(() => {
  menuItems.value = JSON.parse(localStorage.getItem('permissionMenu'))
})
// 树节点的ref
const treeRef = ref(null);

// 控制Dialog组件
const isVisible = ref(false);

// 树节点的数据结构
const defaultProps = {
  children: "children",
  label: "title",
};
// 树结构的数据
const tableData = ref([]);

// 操作的表单数据
const formData = ref({
  title: "", // 菜单主题
  path: "",  // 菜单路径
  type: "", // 类型（首页，菜单，按钮）
  desc: "", //备注
  state:"",  // 状态（是否可使用）
  icon:"",  //图标
  visible:"" // 是否可见
});

// Dialog栏的头部标题
const title = ref("");

// 判断是否是修改操作
const isEdit = ref(false)
// 记录当前操作的数据
const menuData = ref("")

// 点击添加按钮的操作
const append =(data) => {
isVisible.value = true;
menuData.value = data

}

// 新增后的操作
const add = (newChild) => {
  if(menuData.value) {
        treeRef.value.append(newChild,menuData.value) 
      } else {
        menuItems.value.push(newChild)
        //  treeRef.value.append(newChild,menuItems.value)
      }
      ElMessage.success("添加成功");
}

// 修改后的操作
const edited = (newChild) => {
   menuItems.value.forEach(item => {
    if(item.path == menuData.value.path) {
      Object.assign(item,newChild)
    }
  })
   ElMessage.success("修改成功");
}



// 修改/新增的提交操作
const submitMenu = () => {
   const newChild = {...formData.value}
   if(isEdit.value) {
    edited(newChild)
   } else {
      add(newChild)
   }
isVisible.value = false
menuData.value = ""
formData.value = {}
isEdit.value = false
}

// 点击修改按钮后的操作
const editMenu = (data) => {
  isEdit.value = true
  isVisible.value = true
  menuData.value = data
  formData.value = {...formData.value,...data}
}

const deleteOption = (checkData,data) => {
  if(data) {
      treeRef.value.remove(data);
  } else {
    checkData.forEach(item => {
      treeRef.value.remove(item)
  })
   
  }
} 

// 点击确认删除后的操作
const deleteMenu = (node,data) => {
  const checkData = treeRef.value.getCheckedNodes()

  if(checkData.length >0 || data) {
    ElMessageBox.confirm("确定要删除吗？", "提示", {
    type: "warning",
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  })
    .then( () => {
      deleteOption(checkData,data)
      ElMessage.success("删除成功")
    })
    .catch(() => { // 取消时的操作
      menuData.value=""
      ElMessage({
        type: "info",
        message: "取消删除",
      })
    })
  } else {
    ElMessage.error("最少选中一项进行操作")
  }
};




// 编辑/新增取消操作
const cancel = () => {
  isVisible.value = false;
menuData.value=""
formData.value = {}
isEdit.value = false
};

</script>

<style lang="scss" scoped>
.dep_title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .right {
    i {
      font-size: 12px;
      color: #666;
      border: 1px solid #dfdfdf;
      padding: 3px;
      cursor: pointer;
    }
  }
}
.menu {
  display: flex;
  justify-content: space-between;
  &:deep(.el-card__header) {
    padding: 5px;
  }
  &:deep(.el-card__body) {
    padding: 10px 0;
  }
  &:deep(.el-card) {
    border: 0;
  }
  &:deep(.el-tree-node) {
    margin: 20px;
  }
  .menu-left {
    width: 200px;
    margin-right: 10px;
  }
  .menu-right {
    flex: 1;
  }
  .footer {
    display: flex;
    margin-top: 20px;
    align-items: center;
    justify-content: center;
  }
}
.custom-tree-node {
  margin: 10px 0;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
